package uns.fit.gis.ui;

import java.util.Enumeration;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.fileio.FileWrapper;
import uns.fit.gis.fileio.PhotoData;
import uns.fit.gis.mapdrawer.MapCanvas;

public class ImageForm extends Form implements CommandListener, ItemStateListener {
	private MapDrawer midlet;
	private Command backCommand = new Command("Back", Command.BACK, 0);
	private Command saveCommand = new Command("Save image", Command.ITEM, 1);
	private Command viewCommand = new Command("View image", Command.ITEM, 1);
	private ChoiceGroup listImages;
	private byte[] photoData;
	
	public ImageForm(String title, MapDrawer midlet) {
		super(title);
		listImages = new ChoiceGroup("Images", ChoiceGroup.EXCLUSIVE);
		addCommand(backCommand);
		addCommand(saveCommand);
		addCommand(viewCommand);
		this.midlet = midlet;
		setCommandListener(this);
		setItemStateListener(this);
	}
	
	public void itemStateChanged(Item item) {
		if(item == listImages) {
			new Thread() {
				public void run() {
					String str = ImageForm.this.listImages.getString(ImageForm.this.listImages.getSelectedIndex());
					System.out.println(str);
					//FileWrapper fw = new FileWrapper();
					PhotoData photo = new PhotoData();
					FileWrapper.load(photo, "file:///root1/" + str);
					Image image = Image.createImage(photo.getPhotoData(), 0, photo.getPhotoData().length);
					
					ImageForm.this.deleteAll();
					ImageForm.this.append(ImageForm.this.listImages);
					ImageForm.this.append(image);
				}
			}.start();
		}
	}
	
	public void commandAction(Command c, Displayable d) {
		if (c == backCommand) {
			midlet.showMapCanvas();
		}
		else if(c == viewCommand) {
			deleteAll();
			listImages.deleteAll();
			append(listImages);
			new Thread() {
				public void run() {
					//FileWrapper fw = new FileWrapper();
					Enumeration enumeration = FileWrapper.list("file:///root1/", "*.png");
					while(enumeration.hasMoreElements()) {
						String str = enumeration.nextElement().toString();
						System.out.println(str);
						PhotoData photo = new PhotoData(); 
						FileWrapper.load(photo, "file:///root1/" + str);
						Image image = Image.createImage(photo.getPhotoData(), 0, photo.getPhotoData().length);
						listImages.append(str, image);
					}
					//PhotoData data = fw.getImage("Test.img");
					//Image image = Image.createImage(data.getPhotoData(), 0, data.getPhotoData().length);
			    	//append(image);
				}
			}.start();
		}
		else if(c == saveCommand) {
			new Thread() {
				public void run() {
					//FileWrapper fw = new FileWrapper();
					String dirPhotos = System.getProperty("fileconn.dir.photos");
		            if(dirPhotos == null) {
		            	dirPhotos = "file:///root1/";
		            }
					StringBuffer strBuf = new StringBuffer();
					strBuf.append(dirPhotos);
					
					System.out.println(strBuf.toString());
					
					strBuf.append("GeoImage");
					/*
					Date currentDate = new Date();
					strBuf.append(currentDate);
					*/
					strBuf.append(System.currentTimeMillis());
					strBuf.append(".png");
					System.out.println(strBuf.toString());
					FileWrapper.save(new PhotoData(photoData, MapCanvas.currentX, MapCanvas.currentY), strBuf.toString());
					midlet.showAlert("Saved to " + dirPhotos);
				}
			}.start();
			//midlet.showMapCanvas();
		}
	}

	public byte[] getPhotoData() {
		return photoData;
	}

	public void setPhotoData(byte[] photoData) {
		this.photoData = photoData;
		Image image = Image.createImage(this.photoData, 0, photoData.length);
    	append(image);
	}
}
